﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beeldbankmvc.Helpers;
using Beeldbankmvc.Models;

namespace Beeldbankmvc.Controllers
{
    [HandleErrorWithELMAH]
    public class MediaTypeController : Controller
    {
        MediaTypeRepository mediatypeRepository = new MediaTypeRepository();
 
        //
        // GET: /MediaType/
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Index()
        {
            var mediatypes = mediatypeRepository.FindAll().ToList();

            return View("Index", mediatypes);
        }

        //
        // GET: /MediaType/Details/5
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Details(int? id)
        {
            if (id == null)
                return new FileNotFoundResult { Message = "No Media Type found due to invalid id"};

            BEELDBANK_TYPE mediatype = mediatypeRepository.FindOne(id.Value);
            if (mediatype == null)
                return new FileNotFoundResult { Message = "No Media Type found for id=" + id};

            return View(mediatype);
        }

        //
        // GET: /MediaType/Create
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /MediaType/Create
        [HttpPost]
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Create(BEELDBANK_TYPE mediatype)
        {
            if (ModelState.IsValid)
            {
                mediatypeRepository.Add(mediatype);
                mediatypeRepository.Save();

                return RedirectToAction("Details", new { id = mediatype.Id });
            }

            return View();
        }
        
        //
        // GET: /MediaType/Edit/5
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Edit(int id)
        {
            BEELDBANK_TYPE mediatype = mediatypeRepository.FindOne(id);

            if (mediatype == null)
                return new FileNotFoundResult { Message = "No Media Type found for id=" + id };

            return View(mediatype);
        }

        //
        // POST: /MediaType/Edit/5
        [HttpPost]
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            BEELDBANK_TYPE mediatype = mediatypeRepository.FindOne(id);

            try
            {
                UpdateModel(mediatype);
                mediatypeRepository.Save();

                return RedirectToAction("Details", new { id = mediatype.Id });
            }
            catch
            {
                return View(mediatype);
            }
        }

        //
        // GET: /MediaType/Delete/5
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Delete(int id)
        {
            BEELDBANK_TYPE mediatype = mediatypeRepository.FindOne(id);

            if (mediatype == null)
                return new FileNotFoundResult { Message = "No Media Type found for id=" + id };

            return View(mediatype);
        }

        //
        // POST: /MediaType/Delete/5
        [HttpPost]
        [Authorize(Roles = "Administrator,Beheerder,EindRedacteur")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            BEELDBANK_TYPE mediatype = mediatypeRepository.FindOne(id);

            if (mediatype == null)
                return new FileNotFoundResult { Message = "No Media Type found for id=" + id };

            mediatypeRepository.Delete(mediatype);
            mediatypeRepository.Save();

            return View("Index");
        }

        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action " + actionName + " not found");
        }
    }
}
